-module(testS2).
-include_lib("records.hrl").
-export([run/0]).

run() ->
  Main = self(),

  S1 = spawn(fun()->routerc:init(1) end),
  S2 = spawn(fun()->routerc:init(2) end),
  S3 = spawn(fun()->routerc:init(3) end),
  S4 = spawn(fun()->routerc:init(4) end),
  S21 = spawn(fun()->routerc:init(21) end),
  S31 = spawn(fun()->routerc:init(31) end),
  S41 = spawn(fun()->routerc:init(41) end),

  L1_2 = spawn(fun()->routeroutputline:init(1, 5, 1.25*8000, 2, S2, 0)end),
  L1_3 = spawn(fun()->routeroutputline:init(1, 5, 1.25*8000, 3, S3, 0)end),

  L2_e = spawn(fun()->routeroutputline:init(2, 5, 1.25*8000, 21, S21, 0)end),
  L2_4 = spawn(fun()->routeroutputline:init(2, 5, 1.25*8000, 4, S4, 0)end),

  L3_2 = spawn(fun()->routeroutputline:init(3, 5, 1.25*8000, 2, S2, 0)end),
  L3_4 = spawn(fun()->routeroutputline:init(3, 5, 1.25*8000, 4, S4, 0)end),
  L3_e = spawn(fun()->routeroutputline:init(3, 5, 1.25*8000, 31, S31, 0)end),

  L4_e = spawn(fun()->routeroutputline:init(4, 5, 1.25*8000, 41, S41, 0)end),

  S1 ! {self(), {add_output, L1_2}},
  S1 ! {self(), {add_output, L1_3}},

  S2 ! {self(), {add_output, L2_e}},
  S2 ! {self(), {add_output, L2_4}},

  S3 ! {self(), {add_output, L3_2}},
  S3 ! {self(), {add_output, L3_4}},
  S3 ! {self(), {add_output, L3_e}},

  S4 ! {self(), {add_output, L4_e}},

  Utils = spawn(fun()->utils:server()end),

  AllPackets = 100000,
  MeanLength = 8000,

  PG11 = spawn(fun()->packetgenerator_exp:init(Main, AllPackets*0.7, MeanLength, 1.25*0.7, [1,3,31], S1, Utils)end),
  PG12 = spawn(fun()->packetgenerator_exp:init(Main, AllPackets*0.2, MeanLength, 1.25*0.2, [1,3,4,41], S1, Utils)end),
  PG13 = spawn(fun()->packetgenerator_exp:init(Main, AllPackets*0.3, MeanLength, 1.25*0.3, [1,2,4,41], S1, Utils)end),

  PG2 = spawn(fun()->packetgenerator_exp:init(Main, AllPackets*0.25, MeanLength, 1.25*0.25, [2,4,41], S2, Utils)end),

  PG31 = spawn(fun()->packetgenerator_exp:init(Main, AllPackets*0.8, MeanLength, 1.25*0.8, [3,2,21], S3, Utils)end),
  PG32 = spawn(fun()->packetgenerator_exp:init(Main, AllPackets*0.2, MeanLength, 1.25*0.2, [3,4,41], S3, Utils)end),

  MaxTime = utils:get_max(6, 0)+200,

  io:format("Max Time:~p~n", [MaxTime]),
  utils:sleep(2000),

  utils:run_tdm([PG11, PG12, PG13, PG2, PG31, PG32, L1_2, L1_3, L2_e, L2_4, L3_2, L3_4, L3_e, L4_e], 0, MaxTime),


  {_, Loss} = utils:results_tdm([S1,S2,S3,S4]),
  {Rec, _} = utils:results_tdm([S21,S31,S41]),

  utils:display_results_tdm(Rec, Loss),

  utils:close_tdm([S1,S2,S3,S4,S21,S31,S41,PG11, PG12, PG13, PG2, PG31, PG32, L1_2, L1_3, L2_e, L2_4, L3_2, L3_4, L3_e, L4_e, Utils])
  .
